package EncryptionUtils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class RSAUtils {

    private RSAUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 2.RSA加密算法使用 对字符串解密
     *
     * @param info 参数为需要加密的String
     * @return 返回加密后的String
     */
    public static String getRSACode(String info) {
        final Base64.Encoder encoder = Base64.getEncoder();
        final byte[] textByte;
        try {
            textByte = info.getBytes("UTF-8");
            final String encodedText = encoder.encodeToString(textByte);
            return encodedText;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "RSA加密异常";
        }

    }

    /**
     * 3.RSA加密算法使用 对字符串解密
     *
     * @param info 参数为需要加密的String
     * @return 返回解密后的String
     */
    public static String decipheringRSA(String info) {
        final Base64.Decoder decoder = Base64.getDecoder();
        try {
            return new String(decoder.decode(info), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "RSA解密异常";
        }
    }
}
